<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:商品信息管理
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class ProductsAction extends CommonAction {
	function _filter(&$map){
        if($_POST['selectname']){
        $map['prodname|simplename|egname|partnumber|partmodel|prodsize|pmaterial|porigin|pbrand|presale|supinfo|remarks'] = array('like',"%".$_POST['selectname']."%");
        }
        if($_POST['p_typename']){
            $map['p_typename'] = array('like',"%".$_POST['p_typename']."%");
        }
        /*以下高级查询*/
        if($_POST['ad_prodname']){//名称
         $map['prodname']  = array('like',"%".$_POST['ad_prodname']."%");  
        }
        if($_POST['ad_simplename']){//简名
         $map['simplename']  = array('like',"%".$_POST['ad_simplename']."%");  
        }
        if($_POST['ad_egname']){//拼码
         $map['egname']  = array('like',"%".$_POST['ad_egname']."%");  
        }
        if($_POST['ad_p_typename']){//类型
         $map['p_typename']  = array('like',"%".$_POST['ad_p_typename']."%");  
        }
        if($_POST['ad_partnumber']){//料号
         $map['partnumber']  = array('like',"%".$_POST['ad_partnumber']."%");  
        }
        if($_POST['ad_partmodel']){//型号
         $map['partmodel']  = array('like',"%".$_POST['ad_partmodel']."%");  
        }
        if($_POST['ad_prodsize']){//规格
         $map['prodsize']  = array('like',"%".$_POST['ad_prodsize']."%");  
        }
        if($_POST['ad_pmaterial']){//材质
         $map['pmaterial']  = array('like',"%".$_POST['ad_pmaterial']."%");  
        }
        if($_POST['ad_porigin']){//产地
         $map['porigin']  = array('like',"%".$_POST['ad_porigin']."%");  
        }
        if($_POST['ad_pbrand']){//品牌
         $map['pbrand']  = array('like',"%".$_POST['ad_pbrand']."%");  
        }
        if($_POST['ad_spresale']!='' and $_POST['ad_epresale']!=''){
         $map['presale']  = array('between',array($_POST['ad_spresale'],$_POST['ad_epresale']));  
        }
        if(isset($_POST['ad_status'])){
         $map['status']  = $_POST['ad_status'];  
        }
        if($_POST['ad_supinfo']){
         $map['supinfo']  = array('like',"%".$_POST['ad_supinfo']."%");  
        }
        if($_POST['ad_remarks']){
         $map['remarks']  = array('like',"%".$_POST['ad_remarks']."%");  
        }
        if($_POST['ad_screatetime'] and $_POST['ad_ecreatetime']){
         $stime = strtotime($_POST['ad_screatetime']);
         $etime = strtotime($_POST['ad_ecreatetime']);
         $map['createtime']  = array('between',array($stime,$etime));  
        }
        if($_POST['ad_supdatetime'] and $_POST['ad_eupdatetime']){
         $stime = strtotime($_POST['ad_supdatetime']);
         $etime = strtotime($_POST['ad_eupdatetime']);
         $map['updatetime']  = array('between',array($stime,$etime));  
        }      
        /*以上高级查询*/
        $ProModel = D('Products');
        $typeList = $ProModel->field('p_typename')->group('p_typename')->select();
        //dump($typeList);
        $this->assign('typeList',$typeList);    
	}   
 //显示上传图片框
    public function uploadimg(){
        $model = M ('Products');
		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById( $id );
		$this->assign('vo', $vo);
		$this->display();
        //dump($vo);        
    }
    //执行上传图片
    public function uploads(){
        if (!empty($_FILES)) {        
            //如果有文件上传 上传附件            
            $this->_upload('jpg,gif,png,jpeg');
            //$this->forward();
        }else{        
            $this->error('图片上传过程发生异常!');
        }
            
    }
    //显示上传附件框
    public function uploadfiles() {
        $model = M ('Products');
		$id = $_REQUEST[$model->getPk()];
		$vo = $model->getById( $id );
		
        
        $mapf['prodid'] = $id;
        $model = M('Productfile');
        $list = $model->where($mapf)->order('uploadtime desc')->select();
        
        //dump($list);
        $this->assign('vo', $vo);
        $this->assign('list', $list);
        
		$this->display();
        //dump($vo);        
    }
    //执行上传附件
    public function uploadsf(){
        if (!empty($_FILES)) {        
            //如果有文件上传 上传附件            
            $this->_uploadfile();
            //$this->forward();
        }else{        
            $this->error('文件上传过程发生异常!');
        }
            
    }
    //查看商品信息信息
      public function showinfo(){
        $id = $_REQUEST['id'];
        $map['id'] = $id;
        $mapf['prodid'] = $id;
        $PModel = D('Products');
        $FLodel = D('Productfile');
        $listF = $FLodel->where($mapf)->order('uploadtime desc')->select();
        $listP = $PModel->where($map)->find();
        $this->assign('vo', $listP);
        $this->assign('listF', $listF);
        $this->display();
      }
      //删除上传的附件
      public function delFiles(){
        $map['id'] = $_REQUEST['id'];
        $delM = D('Productfile');
        
        $fileurls = $delM->where($map)->getField('fileurl');
        if(unlink('../Public/Uploads/Files/'.$fileurls)){
        if($delM->where($map)->delete()){
            $this->success('成功删除'.$fileurls);
        }else{
            $this->error('删除失败！');
        }
        }else{$this->error('删除附件失败');}
      }
    
    // 文件上传
            
    protected function _uploadfile() {
        import("@.ORG.Util.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',','doc,ttf,pdf,xls');
        //设置附件上传目录
        $upload->savePath = '../Public/Uploads/Files/';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            //$_POST['imageurl'] = $uploadList[0]['savename'];
            $id = $_POST['id'];
            $where['id'] = $id;
            $model = M('Productfile');
            //保存当前数据对象
            
            $data['prodid'] = $id;
            $data['filename'] = $_POST['newfilename'];
            $data['fileurl'] = $uploadList[0]['savename'];
            //$data['create_time'] = time();
            $data['uploadtime'] =time();
            $data['uploaduser'] = $_SESSION[C('USER_AUTH_KEY')];
            
            $list = $model->add($data);
            //dump($data);
            if ($list !== false) {
                $this->success('上传附件成功！');
            } else {
                unlink($uploadList[0]['savepath'].$uploadList[0]['savename']);//数据保存失败则删除上传的附件
                $this->error('上传附件失败!');
            }
        }
        
    }
    //图片上传
    protected function _upload($types) {
        import("@.ORG.Util.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',',$types);
        //设置附件上传目录
        $upload->savePath = '../Public/Uploads/Images/';
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = '@.ORG.Util.Image';
        //设置需要生成缩略图的文件前綴 
        $upload->thumbPrefix = 'm_';//,s_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = '250';
        //设置缩略图最大高度
        $upload->thumbMaxHeight = '250';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除上傳的原图
       // $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            import("@.ORG.Util.Image");
            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
            Image::water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], '../Public/Images/logo2.png');
            //$_POST['imageurl'] = $uploadList[0]['savename'];
            
            $id = $_POST['id'];
            $where['id'] = $id;
            $model = M('Products');
            //保存当前数据对象
            $oldImgUrl = $model->getFieldById($id,'imageurl');
            if($oldImgUrl!=''){
                unlink($uploadList[0]['savepath']."s_".$oldImgUrl);//如果之前有上传则删除图片。by lhp 2012-08-05
                unlink($uploadList[0]['savepath']."m_".$oldImgUrl);
                unlink($uploadList[0]['savepath'].$oldImgUrl);//刪除原圖。
            }
            
            $data['imageurl'] =  $uploadList[0]['savename'];//$_POST['imageurl'];
            //$data['create_time'] = time();
            
            $list = $model->where($where)->save($data);
            //dump($data);
            if ($list !== false) {
            $this->success('上传图片成功！');
            } else {
            $this->error('上传图片失败!');
            }
            
            
        }
        
    }



protected $Execl_Error = array("数据导入成功","找不到文件","Execl文件格式不正确");

public function impExcel(){ 
if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)){ 
    $result = $this->Import_Execl($_FILES["import"]["tmp_name"]); 
    if($this->Execl_Error[$result["error"]] == 0){          
        $execl_data = $result["data"][0]["Content"]; 
        unset($execl_data[1]);
        $count1 = 0;
        $count2 = 0;  
        $data = D("Products"); 
        foreach($execl_data as $k=>$v){ 
            $d["prodname"]   = TRIM($v[0]); 
            $d["simplename"] = TRIM($v[1]); 
            $d["egname"]     = TRIM($v[2]); 
            $d["p_typename"] = TRIM($v[3]); 
            $d["partnumber"] = TRIM($v[4]); 
            $d["partmodel"]  = TRIM($v[5]); 
            $d["prodsize"]   = TRIM($v[6]); 
            $d["pmaterial"]  = TRIM($v[7]); 
            $d["porigin"]    = TRIM($v[8]); 
            $d["pbrand"]     = TRIM($v[9]); 
            $d["unit"]       = TRIM($v[10]); 
            $d["presale"]    = TRIM($v[11]); 
            $d["supinfo"]    = TRIM($v[12]); 
            $d["remarks"]    = TRIM($v[13]); 
            $d["createtime"] = time();
            $d["updatetime"] = time();
            $d["status"]     = 1;
            if($d["prodname"]!='' and $d["p_typename"]!='' and $d["partnumber"]!='' and $d["unit"]!=''){
                if($data->data($d)->add()){
                   $count1 =$count1 + 1;
                }else{$count2 =$count2 + 1;} 
            }else{
                $this->error('终止导入：名称、类型、料号、单位存在空值。行数:'.($k+1));
            }
            
        }
            if($count1>0){
              $this->success('成功导入商品['.$count1.']个，失败['.$count2.']');   
            }else{
                $this->error("导入0条数据,请检查源文件数据是否正确。");
            } 
        
        }else{ 
            $this->error($this->Execl_Error[$result["error"]]); 
        } 
    }else{ 
        $this->error("上传文件失败"); 
    } 
} 
    
    
}/*Action*/
?>